// import * as DuMessage from '@/hooks/element/useEleMessage'

// 单选table
export function useSingleElTable() {
  const currentRow = ref()
  // 改变选中行
  const handleCurrentChange = (currentRowData, oldCurrentRowData) => {
    currentRow.value = currentRowData
  }
  // 判断是否选中行
  const validateCurrentRow = () => {
    return new Promise((resolve, reject) => {
      if (!currentRow.value) {
        DuMessage.warning('请选择需要操作的数据')
        reject('请选择需要操作的数据')
      } else {
        resolve('成功')
      }
    })
  }
  return {
    currentRow,
    validateCurrentRow,
    handleCurrentChange,
  }
}

// 多选table
export function useMultipleTable() {
  const multipleTableRef = ref(null)
  const selectionRows = ref([]) // checkbox选中

  // 改变checkbox事件
  const handleSelectionChange = (val) => {
    selectionRows.value = val
  }

  // 点击行事件
  const handleRowClick = (row, column, event) => {
    multipleTableRef.value.clearSelection()
    multipleTableRef.value.toggleRowSelection(row, true)
  }
  // 判断是否选中数据
  const validateSelectionRows = () => {
    return new Promise((resolve, reject) => {
      if (selectionRows.value.length === 0) {
        DuMessage.warning('请选择需要操作的数据')
        reject('请选择需要操作的数据')
      } else {
        resolve('成功')
      }
    })
  }
  // 判断必须选中一行数据
  const validateSelectionOneRow = () => {
    return new Promise((resolve, reject) => {
      if (selectionRows.value.length !== 1) {
        DuMessage.warning('请选择一行数据进行操作')
        reject('请选择一行数据进行操作')
      } else {
        resolve('成功')
      }
    })
  }
  return {
    multipleTableRef,
    selectionRows,
    handleSelectionChange,
    handleRowClick,
    validateSelectionRows,
    validateSelectionOneRow,
  }
}
